//
//  HMAudioTool.m
//
//
//  Created by apple on 14-8-8.
//  Copyright (c) 2014年 帶頭二哥. All rights reserved.
//

#import "LZAudioTool.h"
#import <AVFoundation/AVFoundation.h>

@implementation LZAudioTool{
    AVAudioPlayer *_player;
    AVAudioPlayer *_shakeplayer;
}

/**
 *  存放所有的音乐播放器
 */
static NSMutableDictionary *_musicPlayers;
+ (NSMutableDictionary *)musicPlayers
{
    if (!_musicPlayers) {
        _musicPlayers = [NSMutableDictionary dictionary];
    }
    return _musicPlayers;
}

/**
 *  播放音乐
 *
 *  @param filename 音乐的文件名
 */
- (BOOL)bgplayMusic:(NSString *)filename
{
    if (!filename) return NO;
    
    _player = [LZAudioTool musicPlayers][filename];
    

    if (!_player) {

        NSURL *url = [[NSBundle mainBundle] URLForResource:filename withExtension:nil];
        if (!url) return NO;
        _player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
        
//       if (![_player prepareToPlay]) return NO;
//       
//        [LZAudioTool musicPlayers][filename] = _player;
//    }
//    
    if (!_player.isPlaying) {
        [_player play];
        [_player setNumberOfLoops:-1];
        }
    }
    return YES;
}

-(BOOL)bgstopMusic{
    
    [_player stop];
    
    return YES;
}



- (BOOL)shakeMusic:(NSString *)filename{
    if (!filename) return NO;
    
    _shakeplayer = [LZAudioTool musicPlayers][filename];
    
    
    if (!_shakeplayer) {
        
        NSURL *url = [[NSBundle mainBundle] URLForResource:filename withExtension:nil];
        if (!url) return NO;
        
        _shakeplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
        
//        if (![_shakeplayer prepareToPlay]) return NO;
//        
//        [LZAudioTool musicPlayers][filename] = _shakeplayer;
//    }
//    
    if (!_shakeplayer.isPlaying) {
        [_shakeplayer play];
        //[_shakeplayer setNumberOfLoops:-1];
    }
    }
    return YES;

}
- (BOOL)shakeStop{
    [_shakeplayer stop];
    
    return YES;
}


@end
